package com.example.demo.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.News;
import com.example.demo.mapper.NewsMapper;
import com.example.demo.service.NewsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class NewsServiceImpl implements NewsService {

    @Resource
    NewsMapper newsMapper;

    @Override
    public IPage<News> selectPage(Integer pageNum, Integer pageSize, String title, String author) {
        QueryWrapper<News> queryWrapper =  new QueryWrapper<>();
        Page<News> page = new Page<>(pageNum,pageSize);
        if (StrUtil.isNotBlank(title)){
            queryWrapper.lambda().like(News::getTitle,title);
        }
        if (StrUtil.isNotBlank(author)){
            queryWrapper.lambda().like(News::getAuthor,author);
        }
        return newsMapper.selectPage(page,queryWrapper);
    }

    @Override
    public Result<?> addNews(News news) {
        news.setDeleted(0);
        newsMapper.insert(news);
        return Result.success();
    }

    @Override
    public Result<?> updateNews(News news) {
        newsMapper.updateById(news);
        return Result.success();
    }

    @Override
    public Result<?> deleteNews(int newsId) {
        newsMapper.deleteById(newsId);
        return Result.success();
    }
}
